var vue = new Vue({
    el: "#main",
    data() {
        return {
            type: 1,
            // getTime: localStorage.getItem("loginTime"),
            getTime: '',
            loginTime: 0,
            obtain: '获取验证码',
            reset_token: '',
            password: '',
            repeatPassword: '',
            phone: '',
            sms_code: '',
            code: '',
            loading: false,
            isDese: false
        }
    },
    mounted() {
        // if (this.getTime != null && this.getTime != 0) {
        // 	this.loginTime = localStorage.getItem("loginTime")
        // 	this.timer()
        // } else {
        // 	this.isDese = false
        // }
    },
    methods: {
        // 获取短信验证码
        obtainBtn() {
            if (this.phone == "") {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '手机号不能为空',
                        type: 'error',
                        duration: 3000
                    });
                }
            } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.phone)) {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '手机号码有误，请重填',
                        type: 'error',
                        duration: 3000
                    });
                }
                // this.loading = false
            } else {
                ajax({
                    url: formal + "/auth/get-reset-pwd-sms-code",
                    type: "POST",
                    data: {
                        phone: this.phone
                    },
                    success: (res) => {
                        if (res.status == 1) {
                            this.code = res.code
                            this.loginTime = 60
                            this.timer()
                            this.isDese = true
                            if (document.getElementsByClassName('el-message').length === 0) {
                                this.$message({
                                    message: res.message,
                                    type: 'success',
                                    duration: 3000
                                });
                            }
                        } else {
                            this.$message({
                                message: res.message,
                                type: 'error',
                                duration: 3000
                            });
                        }
                    }
                })
            }
        },
        // 点击下一步判断手机号，验证码。无误后获取reset_code
        validation() {
            if (this.phone == "") {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '手机号不能为空',
                        type: 'error',
                        duration: 3000
                    });
                }
            } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.phone)) {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '手机号码有误，请重填',
                        type: 'error',
                        duration: 3000
                    });
                }
            } else if (this.sms_code == "") {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '请输入验证码',
                        type: 'error',
                        duration: 3000
                    });
                }
            } else if (this.sms_code != this.code) {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '请输入正确的验证码',
                        type: 'error',
                        duration: 3000
                    });
                }
            } else {
                console.log(11111);
                ajax({
                    url: formal + '/auth/verify-reset-pwd?phone=' + this.phone,
                    type: 'POST',
                    data: {
                        phone: this.phone,
                        sms_code: this.sms_code
                    },
                    success: (res) => {
                        if (res.status == 1) {
                            this.type = 2
                            this.reset_token = res.reset_code
                        } else {
                            if (document.getElementsByClassName('el-message').length === 0) {
                                this.$message({
                                    message: res.message,
                                    type: 'error',
                                    duration: 3000
                                });
                            }
                        }
                    }
                })
            }
        },
        // 密码确认修改
        resetPwd() {
            this.loading = true
            if (this.password.length < 8) {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '密码格式错误',
                        type: 'error',
                        duration: 3000
                    });
                }
                this.loading = false
            } else if (this.repeatPassword != this.password) {
                if (document.getElementsByClassName('el-message').length === 0) {
                    this.$message({
                        message: '请确认密码一致',
                        type: 'error',
                        duration: 3000
                    });
                }
                this.loading = false
            } else {
                ajax({
                    url: formal + '/auth/reset-pwd',
                    type: 'POST',
                    data: {
                        password: this.password,
                        repeatPassword: this.repeatPassword,
                        reset_token: this.reset_token
                    },
                    success: (res) => {
                        if (document.getElementsByClassName('el-message').length === 0) {
                            this.$message({
                                message: res.message,
                                type: 'error',
                                duration: 3000
                            });
                        }
                        this.loading = false
                        window.location.href = "login.html"
                    }
                })
            }
        },
        // /auth/reset-pwd
        // 点击获取验证码增加倒计时
        timer() {
            localStorage.setItem("loginTime", this.loginTime)
            if (this.loginTime > 0) {
                this.disabled = true;
                this.loginTime--;
                this.obtain = this.loginTime + "秒";
                setTimeout(this.timer, 1000);
            } else {
                this.loginTime = 0;
                this.obtain = "获取验证码";
                this.disabled = false;
                this.isDese = false
            }
        }
    },
})
// document.addEventListener('visibilitychange',function(){
//     if(document.visibilityState==='hidden'){
//         console.log("不在当前页");
// 		let time = 6
// 		let setTime
// 		localStorage.setItem("time",time)
// 		console.log(localStorage.getItem('time'))
// 		setTime = setInterval(() => {
// 			time--
// 			if (setTime <= 0) {
// 				clearInterval(setTime)
// 				localStorage.removeItem('loginTime')
// 			}
// 		}, 1000)
//     }
// });